<?php

class Vote_con extends Lxbase{
  //投票的展示
  public function show_vote(){

      $vote_id=$_GET['id'];
     $data_vote=$this->viewdata("*","wxvote","where id=$vote_id");
     $question=$this->arrdata("*","wxquestion","where vote_id=$vote_id");
      if(time()>$data_vote['stop_time']){
          echo "您查看的投票信息已过期!";
          exit;
      }
     if($question){
        foreach ($question as $k => $v){
           $options=unserialize($v['options']);

        }
           foreach ($options as $key=>$item ) {
              $options[$key]['option_key']=$key;    
           }
           $question[$k]['options']=$options;
        }

      if(isset($_COOKIE['vote_'.$data_vote['id']])){
        $this->smarty->assign('user_slec',$_COOKIE['vote_'.$data_vote['id']]);
      }

      $this->smarty->assign("vote",$data_vote);
      $this->smarty->assign("question",$question);
      $this->smarty->display("show_vote.tpl");
   
     
     
  }
  //提交投票
  public function submit_vote(){
    $data=$_POST['data'];//提交的数组

    $vote_id=$_POST['vote_id'];
    if(!empty($data)){
      $quesiton_ids=array();
      $select_option_name=array(); //存放被选过的哪些选项名称
      foreach($data as $k=>$v){
         $quesion_slec_option = explode(',',$v['question_option']);
          $question_id=$v['question_id'];         
         $question=$this->viewdata("*","wxquestion","where id=$question_id");
         $options=unserialize($question['options']);
          foreach($options as $key=>$value){
            if(in_array($key,$quesion_slec_option)){
              $options[$key]['numbers']+=1;
              $select_option_name[]=$value['option_name'];
            }
          }
          $vote_numbers=$question['vote_numbers']+1;
          $options=serialize($options);
          $this->updata("wxquestion","options='{$options}',vote_numbers=$vote_numbers","where id=$question_id");
          $quesiton_ids[]=$v['question_id'];
      }
          $vote=$this->viewdata("*","wxvote","where id=$vote_id");
          $numbers=$vote['numbers']+1;
          $this->updata("wxvote","numbers=$numbers","where id=$vote_id");
         $user_vote_params=array(
            'bs_id'=>$vote['bs_id'],
            'vote_id'=>$vote_id,
            'question_id'=>implode(',',$quesiton_ids),
            'slec_option_key'=>serialize($data),
            'create_time'=>time()
          );
         $this->setdata("user_vote",$user_vote_params);

         setcookie('vote_'.$vote_id,implode(',',$select_option_name));
        
     }
        
         header("location:?index.php&g=c&m=vote&a=show_vote");
  }
}